<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText"></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="项目名称" class="row-padding-bottom">
              <el-input
                type="text"
                placeholder="请填写项目完整名称"
                v-model="filterField.projectName"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="项目状态" class="row-padding-bottom">
              <el-select
                v-model="filterField.projectStatus"
                filterable
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in statusOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="地区" class="row-padding-bottom">
                <el-cascader
                  ref="manageArea"
                  v-model="areaId"
                  :options="areasOptions"
                  @change="getAreaId"
                  clearable
                  filterable
                  placeholder="请选择"
                  :props="{ checkStrictly: true }"
                  :append-to-body="false"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="受理机构" class="row-padding-bottom">
                <el-select
                  v-model="filterField.bureau"
                  filterable
                  placeholder="请选择"
                  clearable
                  no-data-text="请先选择区域"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in bureaus"
                    :key="item.bureau"
                    :label="item.bureau"
                    :value="item.bureau"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item label="">
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
            </el-form-item>
            <el-form-item>
              <exportBtn
                :dataLen="tableList.length"
                :filterField="filterField"
                api="api-s/report/export_JC_CS_INSURANCE"
              ></exportBtn>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableList"
          border
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          @sort-change="sortChange"
          style="border: 1px solid #dfe6ec"
          @header-dragend="handleDragend"
        >
          <el-table-column prop="projectName" label="项目名称" min-width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="projectStatusDesc"
            align="center"
            label="项目状态"
            :formatter="formatTd"
            width="100"
          ></el-table-column>
          <el-table-column
            prop="areaName"
            label="区域"
            align="center"
            :formatter="formatTd"
            width="240"
          ></el-table-column>
          <el-table-column label="最近一次参保受理机构" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.bureau"
                :columnName="'bureau'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="insDate"
            label="最近一次参保时间"
            sortable="custom"
            align="center"
            :formatter="formatTd"
            width="200"
          ></el-table-column>
          <el-table-column
            label="操作"
            align="left"
            fixed="right"
            width="100"
            :formatter="formatTd"
          >
            <template slot-scope="scope">
              <el-link
                type="primary"
                v-if="isShowDetail"
                @click="toDetail(scope.row)"
                >查看</el-link
              >
              <span v-else>--</span>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关工伤保险数据"></emptyTable>
          </div>
        </el-table>

        <!-- getTableList为获取子组件的数据 -->
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
    </div>
  </div>
</template>

<script>
import scrollTable from '@/mixin/scrollTable.js'
import { mapActions, mapState } from 'vuex'

export default {
  name: 'insuranceList',
  mixins: [scrollTable],
  data() {
    return {
      titleText: '工伤保险',
      advanced: false,
      areaId: '',
      bureaus: [],
      //表格数据
      tableList: [],
      requestUrl: 'api-e/cs/insstats/expand/monitor/listbypage',
      //搜索条件
      filterField: {
        projectName: '',
        bureau: '',
        projectStatus: '',
        areaId: '',
      },
      isShow: false, //列表加载
      loading: false,
      isShowDetail: '',
    }
  },
  computed: { ...mapState(['areasOptions', 'statusOptions']) },
  created() {
    this.getAreas({ areaId: this.$store.state.areaInfoArr.areaId })
    if (this.$route.query.status) {
      this.filterField.projectStatus = this.$route.query.status
    }
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    this.isShowDetail =
      this.$store.state.buttonLimitsArr.indexOf('mInsurance:detail') > -1
  },
  activated() {},
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    ...mapActions(['getAreas']),
    //获取选中的区域最后一级id
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId
      this.getBureauData()
    },

    getBureauData() {
      this.bureaus = []
      this.filterField.bureau = ''
      this.axios({
        method: 'post',
        url: 'api-e/cs/insstats/expand/monitor/selectarea',
        data: { areaId: this.filterField.areaId },
      })
        .then((resp) => {
          if (!resp.data.hasError) {
            this.bureaus = resp.data.result
          } else {
            this.$message({
              showClose: true,
              duration: 3000,
              message: resp.data.errorMsg,
              type: 'error',
            })
          }
        })
        .catch((resp) => {
          this.$message.error('数据获取失败')
        })
    },

    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    toDetail(row) {
      this.$router.push({
        path: '/home/insurance/insuranceDetail',
        name: 'insuranceDetail',
        query: {
          id: row.projectId,
          projectName: row.projectName,
          projectStatus: row.projectStatus,
          projectStatusDesc: row.projectStatusDesc,
          projectId: row.projectId,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/insurance/insuranceList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
  },
}
</script>

<style lang="less" scoped>
.mainCon {
  top: 198px;
}
@media only screen and (max-width: 1366px) {
  /deep/.searchInputWidth .el-form--inline .el-form-item__label {
    width: 80px;
  }
}
@import '../../../static/css/filterArea.less';
</style>
